package log;

import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.URL;

public class LogUploader {

    public static void sendLogStream(String log) {
        try {

            //这个地址要跟LoggerController的url一致
            //   URL url = new URL("http://localhost:8081/log2");  //本机部署用localhost:8081
            URL url = new URL("http://localhost:8081/log");  //nginx反向代理负载均衡,达成jar包放多个服务器

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            //设置请求方式为post
            conn.setRequestMethod("POST");

            //时间头用来供server进行时钟校对的
            conn.setRequestProperty("clientTime", System.currentTimeMillis() + "");

            //允许上传数据
            conn.setDoOutput(true);

            //设置请求的头信息,设置内容类型为JSON
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            //输出流
            OutputStream out = conn.getOutputStream();
            out.write(("logString=" + log).getBytes());
            out.flush();
            out.close();
            int code = conn.getResponseCode();
            System.out.println(code + " upload" + log);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
